#include "ConsoleView.h"

ConsoleView::ConsoleView(QWidget *parent) :
    QWidget(parent)
{
    init();
}

void ConsoleView::init() {
    setWindowTitle("Konsola");

    layout = new QVBoxLayout(this);
    layout->setMargin(0);
    layout->setAlignment(0);
    layout->setSpacing(0);

    textConsoleLog = new QTextEdit(this);
    textConsoleLog->setGeometry(0, 0, 640, 370);
    textConsoleLog->setReadOnly(true);
    textConsoleLog->setStyleSheet("background-color: black; color: white; font-size: 16px; font-family: courier");

    QScrollBar * vScrollBar = textConsoleLog->verticalScrollBar();

    textConsoleInput = new QLineEdit(this);
    textConsoleInput->setGeometry(0, 0, 700, 30);
    textConsoleInput->setStyleSheet("font-size: 16px; font-family: courier");
    textConsoleInput->setFocus();

    layout->addWidget(textConsoleLog);
    layout->addWidget(textConsoleInput);

    resize(700, 400);

    connect(textConsoleInput, &QLineEdit::returnPressed, [=](){
        if(textConsoleInput->text().trimmed() != "") {
            print(QString("Command: ") + textConsoleInput->text());
            vScrollBar->setValue(vScrollBar->maximum());
            emit commandSentSignal(textConsoleInput->text().trimmed());
            textConsoleInput->clear();
        }
    });
}

void ConsoleView::print(const QString &communicate) {
    if(communicate != "") {
        textConsoleLog->append("> " + communicate);
    }
}
